<?php
App::uses('AppController', 'Controller');
/**
 * SysSiteGalleries Controller
 *
 * @property SysSiteGallery $SysSiteGallery
 * @property PaginatorComponent $Paginator
 */
class SysSiteGalleriesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index($sys_website_id = null) {
		$this->SysSiteGallery->recursive = 0;
		$sysWebsites = $this->SysSiteGallery->SysWebsite->findById($sys_website_id);
		$this->set('sysWebsites',$sysWebsites);
		$this->set('sysSiteGalleries', $this->Paginator->paginate());
	}

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		if (!$this->SysSiteGallery->exists($id)) {
			throw new NotFoundException(__('Invalid sys site gallery'));
		}
		$options = array('conditions' => array('SysSiteGallery.' . $this->SysSiteGallery->primaryKey => $id));
		$this->set('sysSiteGallery', $this->SysSiteGallery->find('first', $options));
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add($sys_website_id = null) {
		if ($this->request->is('post')) {
			$this->SysSiteGallery->create();
			if ($this->SysSiteGallery->save($this->request->data)) {
				$this->Session->setFlash(__('修改成功'),'tip/tip_float',array('t'=>'success'));
				return $this->redirect(array('action' => 'index',$sys_website_id));
			} else {
				if($this->request->data['SysSiteGallery']['sys_photo_id']){
					$photo = $this->SysSiteGallery->SysPhoto->findById($this->request->data['SysSiteGallery']['sys_photo_id']);
					$this->request->data['SysPhoto'] = $photo['SysPhoto'];
				}
				$this->Session->setFlash(__('修改失败，请重试'),'tip/tip_float');
			}
		}
		$this->set('sys_website_id',$sys_website_id);
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->SysSiteGallery->exists($id)) {
			throw new NotFoundException(__('Invalid sys site gallery'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->SysSiteGallery->save($this->request->data)) {
				$this->Session->setFlash(__('修改成功'),'tip/tip_float',array('t'=>'success'));
				return $this->redirect(array('action' => 'index',$this->request->data['SysSiteGallery']['sys_website_id']));
			} else {
				if($this->request->data['SysSiteGallery']['sys_photo_id']){
					$photo = $this->SysSiteGallery->SysPhoto->findById($this->request->data['SysSiteGallery']['sys_photo_id']);
					$this->request->data['SysPhoto'] = $photo['SysPhoto'];
				}
				$this->Session->setFlash(__('修改失败，请重试'),'tip/tip_float');
			}
		} else {
			$options = array('conditions' => array('SysSiteGallery.' . $this->SysSiteGallery->primaryKey => $id));
			$this->request->data = $this->SysSiteGallery->find('first', $options);
		}
	}

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->SysSiteGallery->id = $id;
		if (!$this->SysSiteGallery->exists()) {
			throw new NotFoundException(__('Invalid sys site gallery'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->SysSiteGallery->delete()) {
			$this->Session->setFlash(__('The sys site gallery has been deleted.'));
		} else {
			$this->Session->setFlash(__('The sys site gallery could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}}
